home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / File / Passwd.php < prev    next >
Encoding:
PHP Script  |  2005-10-16  |  13.2 KB  |  417 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   FileFormats
  16.  * @package    File_Passwd
  17.  * @author     Michael Wallner <mike@php.net>
  18.  * @copyright  2003-2005 Michael Wallner
  19.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version    CVS: $Id: Passwd.php,v 1.25 2005/04/14 15:00:30 mike Exp $
  21.  * @link       http://pear.php.net/package/File_Passwd
  22.  */
  23.  
  24. /**
  25. * Requires PEAR.
  26. */
  27. require_once 'PEAR.php';
  28.  
  29. /**
  30. * Encryption constants.
  31. */
  32. // SHA encryption.
  33. define('FILE_PASSWD_SHA',   'sha');
  34. // MD5 encryption
  35. define('FILE_PASSWD_MD5',   'md5');
  36. // DES encryption
  37. define('FILE_PASSWD_DES',   'des');
  38. // NT hash encryption.
  39. define('FILE_PASSWD_NT',    'nt');
  40. // LM hash encryption.
  41. define('FILE_PASSWD_LM',    'lm');
  42. // PLAIN (no encryption)
  43. define('FILE_PASSWD_PLAIN', 'plain');
  44.  
  45. /**
  46. * Error constants.
  47. */
  48. // Undefined error.
  49. define('FILE_PASSWD_E_UNDEFINED',                   0);
  50. // Invalid file format.
  51. define('FILE_PASSWD_E_INVALID_FORMAT',              1);
  52. define('FILE_PASSWD_E_INVALID_FORMAT_STR',          'Passwd file has invalid format.');
  53. // Invalid extra property.
  54. define('FILE_PASSWD_E_INVALID_PROPERTY',            2);
  55. define('FILE_PASSWD_E_INVALID_PROPERTY_STR',        'Invalid property \'%s\'.');
  56. // Invalid characters.
  57. define('FILE_PASSWD_E_INVALID_CHARS',               3);
  58. define('FILE_PASSWD_E_INVALID_CHARS_STR',           '%s\'%s\' contains illegal characters.');
  59. // Invalid encryption mode.
  60. define('FILE_PASSWD_E_INVALID_ENC_MODE',            4);
  61. define('FILE_PASSWD_E_INVALID_ENC_MODE_STR',        'Encryption mode \'%s\' not supported.');
  62. // Exists already.
  63. define('FILE_PASSWD_E_EXISTS_ALREADY',              5);
  64. define('FILE_PASSWD_E_EXISTS_ALREADY_STR',          '%s\'%s\' already exists.');
  65. // Exists not.
  66. define('FILE_PASSWD_E_EXISTS_NOT',                  6);
  67. define('FILE_PASSWD_E_EXISTS_NOT_STR',              '%s\'%s\' doesn\'t exist.');
  68. // User not in group.
  69. define('FILE_PASSWD_E_USER_NOT_IN_GROUP',           7);
  70. define('FILE_PASSWD_E_USER_NOT_IN_GROUP_STR',       'User \'%s\' doesn\'t exist in group \'%s\'.');
  71. // User not in realm.
  72. define('FILE_PASSWD_E_USER_NOT_IN_REALM',           8);
  73. define('FILE_PASSWD_E_USER_NOT_IN_REALM_STR',       'User \'%s\' doesn\'t exist in realm \'%s\'.');
  74. // Parameter must be of type array.
  75. define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY',         9);
  76. define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR',     'Parameter %s must be of type array.');
  77. // Method not implemented.
  78. define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED',      10);
  79. define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR',  'Method \'%s()\' not implemented.');
  80. // Directory couldn't be created.
  81. define('FILE_PASSWD_E_DIR_NOT_CREATED',             11);
  82. define('FILE_PASSWD_E_DIR_NOT_CREATED_STR',         'Couldn\'t create directory \'%s\'.');
  83. // File couldn't be opened.
  84. define('FILE_PASSWD_E_FILE_NOT_OPENED',             12);
  85. define('FILE_PASSWD_E_FILE_NOT_OPENED_STR',         'Couldn\'t open file \'%s\'.');
  86. // File coudn't be locked.
  87. define('FILE_PASSWD_E_FILE_NOT_LOCKED',             13);
  88. define('FILE_PASSWD_E_FILE_NOT_LOCKED_STR',         'Couldn\'t lock file \'%s\'.');
  89. // File couldn't be unlocked.
  90. define('FILE_PASSWD_E_FILE_NOT_UNLOCKED',           14);
  91. define('FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR',       'Couldn\'t unlock file.');
  92. // File couldn't be closed.
  93. define('FILE_PASSWD_E_FILE_NOT_CLOSED',             15);
  94. define('FILE_PASSWD_E_FILE_NOT_CLOSED_STR',         'Couldn\'t close file.');
  95.  
  96. /**
  97. * Allowed 64 chars for salts
  98. */
  99. $GLOBALS['_FILE_PASSWD_64'] =
  100.     './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  101.  
  102. /**
  103. * The package File_Passwd provides classes and methods 
  104. * to handle many different kinds of passwd files.
  105. * The File_Passwd class in certain is a factory container for its special 
  106. * purpose extension classes, each handling a specific passwd file format.
  107. * It also provides a static method for reasonable fast user authentication.
  108. * Beside that it offers some encryption methods used by the extensions.
  109. *
  110. * @author       Michael Wallner <mike@php.net>
  111. * @version      $Revision: 1.25 $
  112. * Usage Example:
  113. * <code>
  114. *  $passwd = &File_Passwd::factory('Unix');
  115. * </code>
  116. */
  117. class File_Passwd
  118. {
  119.     /**
  120.     * Get API version
  121.     *
  122.     * @static
  123.     * @access   public
  124.     * @return   string          API version
  125.     */
  126.     function apiVersion()
  127.     {
  128.         return '1.0.0';
  129.     }
  130.  
  131.     /**
  132.     * Generate salt
  133.     *
  134.     * @access   public
  135.     * @return   mixed
  136.     * @param    int     $length     salt length
  137.     * @return   string  the salt
  138.     */
  139.     function salt($length = 2)
  140.     {
  141.         $salt = '';
  142.         $length = (int) $length;
  143.         $length < 2 && $length = 2;
  144.         for($i = 0; $i < $length; $i++) {
  145.             $salt .= $GLOBALS['_FILE_PASSWD_64'][rand(0, 63)];
  146.         }
  147.         return $salt;
  148.     }
  149.  
  150.     /**
  151.     * No encryption (plaintext)
  152.     *
  153.     * @access   public
  154.     * @return   string  plaintext input
  155.     * @param    string  plaintext passwd
  156.     */
  157.     function crypt_plain($plain)
  158.     {
  159.         return $plain;
  160.     }
  161.     
  162.     /**
  163.     * DES encryption
  164.     *
  165.     * @static
  166.     * @access   public
  167.     * @return   string  crypted text
  168.     * @param    string  $plain  plaintext to encrypt
  169.     * @param    string  $salt   the salt to use for encryption (2 chars)
  170.     */
  171.     function crypt_des($plain, $salt = null)
  172.     {
  173.         (is_null($salt) || strlen($salt) < 2) && $salt = File_Passwd::salt(2);
  174.         return crypt($plain, $salt);
  175.     }
  176.     
  177.     /**
  178.     * MD5 encryption
  179.     *
  180.     * @static
  181.     * @access   public
  182.     * @return   string  crypted text
  183.     * @param    string  $plain  plaintext to encrypt
  184.     * @param    string  $salt   the salt to use for encryption 
  185.     *                           (>2 chars starting with $1$)
  186.     */
  187.     function crypt_md5($plain, $salt = null)
  188.     {
  189.         if (
  190.             is_null($salt) || 
  191.             strlen($salt) < 3 || 
  192.             !preg_match('/^\$1\$/', $salt))
  193.         {
  194.             $salt = '$1$' . File_Passwd::salt(8);
  195.         }
  196.         return crypt($plain, $salt);
  197.     }
  198.     
  199.     /**
  200.     * SHA1 encryption
  201.     *
  202.     * Returns a PEAR_Error if sha1() is not available (PHP<4.3).
  203.     * 
  204.     * @static
  205.     * @throws   PEAR_Error
  206.     * @access   public
  207.     * @return   mixed   crypted string or PEAR_Error
  208.     * @param    string  $plain  plaintext to encrypt
  209.     */
  210.     function crypt_sha($plain)
  211.     {
  212.         if (!function_exists('sha1')) {
  213.             return PEAR::raiseError(
  214.                 'SHA1 encryption is not available (PHP < 4.3).',
  215.                 FILE_PASSWD_E_INVALID_ENC_MODE
  216.             );
  217.         }
  218.         $hash = PEAR_ZE2 ? sha1($plain, true) : pack('H40', sha1($plain));
  219.         return '{SHA}' . base64_encode($hash);
  220.  
  221.     }
  222.         
  223.     /**
  224.     * APR compatible MD5 encryption
  225.     *
  226.     * @access   public
  227.     * @return   mixed
  228.     * @param    string  $plain  plaintext to crypt
  229.     * @param    string  $salt   the salt to use for encryption
  230.     */
  231.     function crypt_apr_md5($plain, $salt = null)
  232.     {
  233.         if (is_null($salt)) {
  234.             $salt = File_Passwd::salt(8);
  235.         } elseif (preg_match('/^\$apr1\$/', $salt)) {
  236.             $salt = preg_replace('/^\$apr1\$([^$]+)\$.*/', '\\1', $salt);
  237.         } else {
  238.             $salt = substr($salt, 0,8);
  239.         }
  240.         
  241.         $length  = strlen($plain);
  242.         $context = $plain . '$apr1$' . $salt;
  243.         
  244.         if (PEAR_ZE2) {
  245.             $binary = md5($plain . $salt . $plain, true);
  246.         } else {
  247.             $binary = pack('H32', md5($plain . $salt . $plain));
  248.         }
  249.         
  250.         for ($i = $length; $i > 0; $i -= 16) {
  251.             $context .= substr($binary, 0, min(16 , $i));
  252.         }
  253.         for ( $i = $length; $i > 0; $i >>= 1) {
  254.             $context .= ($i & 1) ? chr(0) : $plain[0];
  255.         }
  256.         
  257.         $binary = PEAR_ZE2 ? md5($context, true) : pack('H32', md5($context));
  258.         
  259.         for($i = 0; $i < 1000; $i++) {
  260.             $new = ($i & 1) ? $plain : $binary;
  261.             if ($i % 3) {
  262.                 $new .= $salt;
  263.             }
  264.             if ($i % 7) {
  265.                 $new .= $plain;
  266.             }
  267.             $new .= ($i & 1) ? $binary : $plain;
  268.             $binary = PEAR_ZE2 ? md5($new, true) : pack('H32', md5($new));
  269.         }
  270.         
  271.         $p = array();
  272.         for ($i = 0; $i < 5; $i++) {
  273.             $k = $i + 6;
  274.             $j = $i + 12;
  275.             if ($j == 16) {
  276.                 $j = 5;
  277.             }
  278.             $p[] = File_Passwd::_64(
  279.                 (ord($binary[$i]) << 16) |
  280.                 (ord($binary[$k]) << 8) |
  281.                 (ord($binary[$j])),
  282.                 5
  283.             );
  284.         }
  285.         
  286.         return 
  287.             '$apr1$' . $salt . '$' . implode($p) . 
  288.             File_Passwd::_64(ord($binary[11]), 3);
  289.     }
  290.  
  291.     /**
  292.     * Convert hexadecimal string to binary data
  293.     *
  294.     * @static
  295.     * @access   private
  296.     * @return   mixed
  297.     * @param    string  $hex
  298.     */
  299.     function _hexbin($hex)
  300.     {
  301.         $rs = '';
  302.         $ln = strlen($hex);
  303.         for($i = 0; $i < $ln; $i += 2) {
  304.             $rs .= chr(hexdec($hex{$i} . $hex{$i+1}));
  305.         }
  306.         return $rs;
  307.     }
  308.     
  309.     /**
  310.     * Convert to allowed 64 characters for encryption
  311.     *
  312.     * @static
  313.     * @access   private
  314.     * @return   string
  315.     * @param    string  $value
  316.     * @param    int     $count
  317.     */
  318.     function _64($value, $count)
  319.     {
  320.         $result = '';
  321.         while(--$count) {
  322.             $result .= $GLOBALS['_FILE_PASSWD_64'][$value & 0x3f];
  323.             $value >>= 6;
  324.         }
  325.         return $result;
  326.     }
  327.  
  328.     /**
  329.     * Factory for new extensions
  330.     * 
  331.     * o Unix        for standard Unix passwd files
  332.     * o CVS         for CVS pserver passwd files
  333.     * o SMB         for SMB server passwd files
  334.     * o Authbasic   for AuthUserFiles
  335.     * o Authdigest  for AuthDigestFiles
  336.     * o Custom      for custom formatted passwd files
  337.     * 
  338.     * Returns a PEAR_Error if the desired class/file couldn't be loaded.
  339.     * 
  340.     * @static   use &File_Passwd::factory() for instantiating your passwd object
  341.     * 
  342.     * @throws   PEAR_Error
  343.     * @access   public
  344.     * @return   object    File_Passwd_$class - desired Passwd object
  345.     * @param    string    $class the desired subclass of File_Passwd
  346.     */
  347.     function &factory($class)
  348.     {
  349.         $class = ucFirst(strToLower($class));
  350.         if (!@include_once "File/Passwd/$class.php") {
  351.             return PEAR::raiseError("Couldn't load file Passwd/$class.php", 0);
  352.         }
  353.         $class = 'File_Passwd_'.$class;
  354.         if (!class_exists($class)) {
  355.             return PEAR::raiseError("Couldn't load class $class.", 0);
  356.         }
  357.         $instance = &new $class();
  358.         return $instance;
  359.     }
  360.     
  361.     /**
  362.     * Fast authentication of a certain user
  363.     * 
  364.     * Though this approach should be reasonable fast, it is NOT
  365.     * with APR compatible MD5 encryption used for htpasswd style
  366.     * password files encrypted in MD5. Generating one MD5 password
  367.     * takes about 0.3 seconds!
  368.     * 
  369.     * Returns a PEAR_Error if:
  370.     *   o file doesn't exist
  371.     *   o file couldn't be opened in read mode
  372.     *   o file couldn't be locked exclusively
  373.     *   o file couldn't be unlocked (only if auth fails)
  374.     *   o file couldn't be closed (only if auth fails)
  375.     *   o invalid <var>$type</var> was provided
  376.     *   o invalid <var>$opt</var> was provided
  377.     * 
  378.     * Depending on <var>$type</var>, <var>$opt</var> should be:
  379.     *   o Smb:          encryption method (NT or LM)
  380.     *   o Unix:         encryption method (des or md5)
  381.     *   o Authbasic:    encryption method (des, sha or md5)
  382.     *   o Authdigest:   the realm the user is in
  383.     *   o Cvs:          n/a (empty)
  384.     *   o Custom:       array of 2 elements: encryption function and delimiter
  385.     * 
  386.     * @static   call this method statically for a reasonable fast authentication
  387.     * 
  388.     * @throws   PEAR_Error
  389.     * @access   public
  390.     * @return   return      mixed   true if authenticated, 
  391.     *                               false if not or PEAR_error
  392.     * 
  393.     * @param    string      $type   Unix, Cvs, Smb, Authbasic or Authdigest
  394.     * @param    string      $file   path to passwd file
  395.     * @param    string      $user   the user to authenticate
  396.     * @param    string      $pass   the plaintext password
  397.     * @param    mixed       $opt    o Smb:          NT or LM
  398.     *                               o Unix:         des or md5
  399.     *                               o Authbasic     des, sha or md5
  400.     *                               o Authdigest    realm the user is in
  401.     *                               o Custom        encryption function and
  402.     *                                               delimiter character
  403.     */
  404.     function staticAuth($type, $file, $user, $pass, $opt = '')
  405.     {
  406.         $type = ucFirst(strToLower($type));
  407.         if (!@include_once "File/Passwd/$type.php") {
  408.             return PEAR::raiseError("Couldn't load file Passwd/$type.php", 0);
  409.         }
  410.         $func = array('File_Passwd_' . $type, 'staticAuth');
  411.         return call_user_func($func, $file, $user, $pass, $opt);
  412.     }
  413. }
  414. ?>
  415.